package com.office.automate.servlet.employee;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.office.automate.dto.convertors.EmployeeDetailsConvertor;
import com.office.automate.exception.framework.FrontierVisibleException;
import com.office.automate.model.authenticate.UserAuthenticator;
import com.office.automate.model.employee.Employee;
import com.office.automate.model.employee.EmployeeOverTime;
import com.office.automate.service.def.employee.EmployeeService;
import com.office.automate.service.impl.employee.EmployeeServiceImplementer;
import com.office.automate.statics.ApplicationStatics;
import com.office.automate.validation.framework.ValidatorObject;

/**
 * Servlet implementation class RequestOverTimeServlet
 */
@WebServlet("/RequestOverTimeServlet")
public class RequestOverTimeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RequestOverTimeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/employee/ApplyForOverTime.jsp");
		try {
			EmployeeService employeeService = new EmployeeServiceImplementer();
			UserAuthenticator currentUser = (UserAuthenticator) (request.getSession().getAttribute("currentSessionUser"));
			EmployeeOverTime employeeOverTime = new EmployeeDetailsConvertor().convertEmployeeOTRequest(request);
			Employee employee = employeeService.getEmployeeDetails(currentUser.getEmployeeId());
			employeeOverTime.setEmployeeId(currentUser.getEmployeeId());
			employeeOverTime.setApproverEmployeeId(employee.getManagerId());
			employeeOverTime.setRequestedDate(new Date());
			employeeOverTime.setStatus(ApplicationStatics.PENDING_APPROVAL);
			HashMap<String, FrontierVisibleException> fieldValidationErrors = 
												employeeService.validateOTRequestDetails(employeeOverTime);
			if (fieldValidationErrors != null) {
				request.setAttribute("EmployeeOverTime", employeeOverTime);
				request.setAttribute("FieldValidationErrors", new ValidatorObject(fieldValidationErrors));
			} else {
				new EmployeeServiceImplementer().addEmployeeOTRequest(employeeOverTime);
			}
			dispatcher.forward(request, response);				
		} catch (FrontierVisibleException e) {
			request.setAttribute("OperationalError", e);
			dispatcher.forward(request, response);
		}
	}

}
